package org.huhuiyu.easyui.action;

import java.util.List;

import org.apache.log4j.Logger;
import org.huhuiyu.easyui.enity.DeptModel;
import org.huhuiyu.easyui.service.DeptService;

import com.opensymphony.xwork2.Action;

public class DeptAction {
	private static Logger logger = Logger.getLogger(DeptAction.class);
	private DeptService deptService;
	private DeptModel deptModel;
	private String actionMessage;

	public DeptAction() {
	}

	public void setDeptService(DeptService deptService) {
		this.deptService = deptService;
	}

	public DeptService getDeptService() {
		return deptService;
	}

	public DeptModel getDeptModel() {
		return deptModel;
	}

	public void setDeptModel(DeptModel deptModel) {
		this.deptModel = deptModel;
	}

	public String getActionMessage() {
		return actionMessage;
	}

	public void setActionMessage(String actionMessage) {
		this.actionMessage = actionMessage;
	}

	public String queryAll() throws Exception {
		List<DeptModel> list = deptService.queryAllDeptModel();
		logger.debug(list);
		JsonActionUtil util = JsonActionUtil.getSuccessJson("rows", list);
		util.write();
		return Action.NONE;
	}

	public String save() throws Exception {
		logger.debug(deptModel);
		JsonActionUtil util = new JsonActionUtil();
		try {
			deptService.saveDeptModel(deptModel);
			util.putData("message", "添加成功！");
			util.setSuccess(true);
		} catch (Exception e) {
			util.putData("message", "添加失败！");
			util.setSuccess(false);
			logger.fatal("程序发生错误", e);
		}
		util.write();
		return Action.NONE;
	}

	public String delete() throws Exception {
		logger.debug(deptModel);
		JsonActionUtil util = new JsonActionUtil();
		try {
			deptModel = deptService.queryDeptModelByKey(deptModel);
			if (deptModel != null) {
				deptService.deleteDeptModel(deptModel);
			}
			util.putData("message", "删除成功！");
			util.setSuccess(true);
		} catch (Exception e) {
			util.putData("message", "删除失败！");
			util.setSuccess(false);
			logger.fatal("程序发生错误", e);
		}
		util.write();
		return Action.NONE;
	}

	public String queryByKey() throws Exception {
		logger.debug(deptModel);
		deptModel = deptService.queryDeptModelByKey(deptModel);
		logger.debug(deptModel);
		JsonActionUtil util = JsonActionUtil.getSuccessJson("data", deptModel);
		util.write();
		return Action.NONE;
	}

	public String modify() throws Exception {
		logger.debug(deptModel);
		JsonActionUtil util = new JsonActionUtil();
		try {
			deptService.updateDeptModel(deptModel);
			util.putData("message", "修改成功！");
			util.setSuccess(true);
		} catch (Exception e) {
			util.putData("message", "修改失败！");
			util.setSuccess(false);
			logger.fatal("程序发生错误", e);
		}
		util.write();
		return Action.NONE;
	}

}
